<template>
  <div class="box-mains">
    <el-form ref="menuForm" :model="menuForm" label-width="100px">
      <el-form-item label="角色名称">
        <el-input disabled v-model="menuForm.name"></el-input>
      </el-form-item>
      <el-form-item label="数据权限">
        <el-checkbox
          v-model="menuForm.menuCheckStrictly"
          @change="handleCheckedTreeConnect($event, 'menu')"
          >父子联动</el-checkbox
        >
        <el-tree
          class="tree-border"
          :data="menuTreeList"
          :check-strictly="!menuForm.menuCheckStrictly"
          show-checkbox
          default-expand-all
          ref="tree"
          node-key="id"
          empty-text="加载中，请稍候"
          :props="defaultProps"
        ></el-tree>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" @click="handleSave()"
          >保存</el-button
        >
        <el-button size="small" @click="handleClear()">清空</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { fetchTreeList } from "@/api/menu";
import { listMenuByRole, allocMenu } from "@/api/role";
import { getRoleInfo } from "@/api/dept";

export default {
  name: "allocMenu",
  data() {
    return {
      menuTreeList: [],
      defaultProps: {
        children: "children",
        label: "title"
      },
      roleId: null,
      menuForm: {
        menuCheckStrictly: true,
        menuIds: [],
        name: ""
      }
    };
  },
  created() {
    this.roleId = this.$route.query.roleId;
    this.menuForm.name = this.$route.query.name;
    this.treeList();
    this.getRoleMenu(this.roleId);
  },
  methods: {
    handleCheckedTreeConnect(value, type) {
      if (type == "menu") {
        this.menuForm.menuCheckStrictly = value ? true : false;
      }
    },
    treeList() {
      fetchTreeList().then(response => {
        this.menuTreeList = response.data;
      });
    },
    async getRoleMenu(roleId) {
      const res = await getRoleInfo(roleId);
      this.menuForm.menuCheckStrictly = res.data.menuCheckStrictly;
      listMenuByRole(roleId).then(response => {
        let menuList = response.data;
        let checkedMenuIds = [];
        if (menuList != null && menuList.length > 0) {
          for (let i = 0; i < menuList.length; i++) {
            let menu = menuList[i];
            checkedMenuIds.push(menu.id);
            // if (menu.parentId !== 0) {
            //   checkedMenuIds.push(menu.id);
            // }
          }
        }
        this.$refs.tree.setCheckedKeys(checkedMenuIds);
      });
    },
    getMenuAllCheckedKeys() {
      let checkedKeys = this.$refs.tree.getCheckedKeys();
      let halfCheckedKeys = this.$refs.tree.getHalfCheckedKeys();
      checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
      return checkedKeys;
    },
    handleSave() {
      let roles = this.getMenuAllCheckedKeys();

      let params = Object.assign(this.menuForm);
      params.roleId = Number(this.roleId);
      params.menuIds = roles.join(",");
      delete params.name;
      this.$confirm("是否分配菜单?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        allocMenu(params).then(res => {
          this.$message.success("分配成功");
          this.menuShow = false;
          this.$router.go(-1);
        });
      });
    },
    handleClear() {
      this.$refs.tree.setCheckedKeys([]);
    }
  }
};
</script>

<style scoped>
.box-mains {
  padding: 30px;
  width: 40%;
}
.tree-border {
  border: 1px solid #ddd;
}
</style>
